
package com.ukao.steward.pesenter.valetRunners;

import com.ukao.steward.base.BasePresenter;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.bean.WaitingOrderBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.T;
import com.ukao.steward.view.valetRunners.WaitingOrderView;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class WaitingOrderPesenter extends BasePresenter<WaitingOrderView> {


    public WaitingOrderPesenter(WaitingOrderView view) {
        attachView(view);
    }

    /**
     待接单列表
     */
    public void waitOrder(  int pageNum ,int pageSize) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("pageNum",pageNum+"");
        mHashMap.put("pageSize",pageSize+"");
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.waitOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<WaitingOrderBean>() {
                    @Override
                    public void onSuccess(WaitingOrderBean model) {

                        if (model.getHttpCode() == 200) {

                            mvpView.waitOrderSucceed(model.getData());
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                       mvpView.loadfinish();

                    }
                });

        }








    /**
     接收订单
     */
    public void receiving(final    WaitingOrderBean.ListBean   mListBean) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId",mListBean.getId()+"");
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.receiving(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {

                            mvpView.receivingSucceed(mListBean,model.getMsg());
                        }else{
                            mvpView.receivingFail(mListBean.getId(),model.getMsg());

                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }
    /**
     拒绝接单
     */
    public void reject(  final    WaitingOrderBean.ListBean   mListBean) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId",mListBean.getId()+"");
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.reject(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {

                            mvpView.receivingSucceed(mListBean,model.getMsg());
                        }else  if (model.getHttpCode() == 400){

                            mvpView.receivingFail(mListBean.getId(),model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }



    /**
     *  设置开工状态
     */
    public void setSatrtWorking(  ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        Observable    observable=apiStores.onLine (Constant.createParameter(mHashMap));
        addSubscription(observable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            SaveParams.saveIsRest(true);
                            mvpView.satrtWorkingSucceed();
                        } else {


                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }
}
